package com.example.easy.excel.service.serviceimpl;

import com.alibaba.excel.metadata.BaseRowModel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.easy.excel.entity.User;
import com.example.easy.excel.dao.UserDao;
import com.example.easy.excel.entity.db.UserDB;
import com.example.easy.excel.entity.sheet.UserSheet;
import com.example.easy.excel.excel.ExcelUtil;
import com.example.easy.excel.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {
   @Resource
    private UserDao userDao;

    @Override
    public List<User> exportUserExcel()  {
       return userDao.selectAll();
    }

    /**
     * 导入 Excel（一个 sheet）到数据库
     * 这里可以处理很多业务逻辑,比如传入的id不能重复,或者把插入失败的数据返回给客户端
     *
     * @param excel       需要导入的excel
     * @param headLineNum
     * @return
     */
    @Override
    public List<Object> importUserExcel(MultipartFile excel, BaseRowModel rowModel, int headLineNum) {
        if (excel == null) {
            log.error("请传入正确的excel格式");
            return null;
        }
        List<Object> list = ExcelUtil.readExcel(excel, rowModel, headLineNum);
        User user = new User();
        for (int i = 0; i < list.size(); i++) {
            UserDB userDB = (UserDB) list.get(i);
             //user.setUserId(userDB.getUser_id());
            user.setName(userDB.getName());
            user.setAge(userDB.getAge());
            ///String birthday = userDB.getBirthday();

            user.setBirthday(userDB.getBirthday());
            user.setSex(userDB.getSex());
            user.setHeight(userDB.getHeight());
            user.setAddress(userDB.getAddress());
            //    可以写很多处理后的数据
            user.setCredits(userDB.getCredits());

            user.setCreateTime(userDB.getCreateTime());
            userDao.insert(user);
        }
        return ExcelUtil.readExcel(excel, new UserDB(), headLineNum);
    }

    /**
     * 导入多sheet的 excel
     *
     * @param excel
     * @param rowModel
     * @param sheetNo
     * @param headLineNum
     * @return
     */
    @Override
    public List<Object> inportManySheet(MultipartFile excel, BaseRowModel rowModel, int sheetNo, int headLineNum) {
        if (excel == null) {
            log.error("请传入正确的excel格式");
            return null;
        }
        List<Object> list = ExcelUtil.readExcel(excel, new UserDB(), sheetNo, headLineNum);
        User user = new User();
        try {
            for (int i = 0; i < list.size(); i++) {
                UserDB userDB = (UserDB) list.get(i);
               // user.setUserId(userDB.getUser_id());
                user.setName(userDB.getName());
                user.setAge(userDB.getAge());
                user.setBirthday(userDB.getBirthday());
                user.setSex(userDB.getSex());
                user.setHeight(userDB.getHeight());
                user.setAddress(userDB.getAddress());
                //    可以写很多处理后的数据
                user.setCredits(userDB.getCredits());

                user.setCreateTime(userDB.getCreateTime());
                userDao.insert(user);
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
            log.error("导入数据库失败,请检查参数和查看控制台!");
            return null;
        }
        return ExcelUtil.readExcel(excel, new UserDB(), headLineNum);
    }


}
